import { cancelWxBusiCoupon } from '@src/api/coupon'
import base from "@src/store/base";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { getCard } from '@src/api/member'
import router from "@src/router";
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  options: {
    addGlobalClass: true
  },
  properties: {
    couponDetail: {
      type: Object,
      value: null
    }
  },
  data: {
    //兑换按钮显示的颜色
    flag: true,
    showDialog: false,
    codes: null,
    type: null,
    secret: null
  },
  methods: {
    dialogShow(params) {
      this.setData({ showDialog: true, codes: params.code, type: params.type })
    },
    bindinputFunc(val) {
      let len = val.detail.cursor;
      let value = val.detail.value
      if (len > 5) { this.setData({ flag: false, secret: value }); } else { this.setData({ flag: true }) }
    },
    hiddenbtn() {
      this.setData({ showDialog: false })
    },
    handleExchange() {
      let self = this;
      let data = {
        cardNo: this.data.codes,
        secret: this.data.secret
      }
      getCard(data).then(res => {
        if (res.code == 0) {
          if (res.data.finish) {
            self.hiddenbtn()
            wx.showModal({
              title: '兑换成功',
              content: '成功领取卡',
              confirmText: '查看详情',
              cancelText: '返回',
              success(res) {
                if (res.confirm) {
                  //去详情页
                  console.log('用户点击确定')
                  router.push({ name: 'member/couponDetailNew', params: { type: 2, model: 1, code: self.data.codes } })
                } else if (res.cancel) {
                  //返回
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      }).catch(error => {
        wx.showToast({
          title: error.data.msg,
          icon: 'none',
          duration: 2000
        })
      })
    }



  }
})
